<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <title>用户注册</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: "Segoe UI", sans-serif;
            background-color: #f4f6f9;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .register-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }

        .register-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            font-weight: bold;
            margin-bottom: 5px;
        }

        input[type="text"],
        input[type="password"],
        input[type="email"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>

<div class="register-container">
    <h2>用户注册</h2>
    <div class="form-group">
        <label for="userName">用户名：</label>
        <input type="text" id="userName" placeholder="请输入用户名" required />
    </div>
    <div class="form-group">
        <label for="password">密码：</label>
        <input type="password" id="password" placeholder="请输入密码" required />
    </div>
    <div class="form-group">
        <label for="pid">身份证号：</label>
        <input type="text" id="pid" placeholder="请输入身份证号" />
    </div>
    <div class="form-group">
        <label for="email">邮箱：</label>
        <input type="email" id="email" placeholder="请输入邮箱" />
    </div>
    <div class="form-group">
        <label for="mobile">联系电话：</label>
        <input type="text" id="mobile" placeholder="请输入联系电话" />
    </div>
    <div class="form-group">
        <label for="address">居住地址：</label>
        <input type="text" id="address" placeholder="请输入居住地址" />
    </div>
    <button onclick="submitRegister()">立即注册</button>
</div>

<script>
    function submitRegister() {
        const user = {
            userName: $('#userName').val(),
            password: $('#password').val(),
            pid: $('#pid').val(),
            email: $('#email').val(),
            mobile: $('#mobile').val(),
            address: $('#address').val()
        };

        $.ajax({
            url: '/api/users',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(user),
            success: function(response) {
                alert("注册成功！");
                window.location.href = "denglu.html"; // 跳转登录页
            },
            error: function(xhr, status, error) {
                alert("注册失败：" + xhr.responseText);
            }
        });
    }
</script>

</body>
</html>